<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>租赁管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="carFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="carFormData.sname" placeholder="请输入汽车门店"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="sname"
                            label="门店名称"
                            width="180">
                    </el-table-column>

                    <el-table-column
                            prop="address"
                            label="门店地址">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="添加时间">
                    </el-table-column>

                    <el-table-column
                            prop="ishot"
                            :formatter="zhuangtaima"
                            label="是否品牌制造商">
                    </el-table-column>


                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--        提示框-->
        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                :before-close="handleClose"
        >

            <!--            model:表单数据-->
            <el-form  label-width="80px" :model="carForm" :rules="insertFormRules"  ref="insertFormRef">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="carForm.id" ></el-input>
                </el-form-item>

                <el-form-item label="省市县" prop="provinceId" >
                    <template>
                        <div id="app">
                            <el-cascader
                                    size="large"
                                    :options="options"
                                    v-model="selectedOptions"
                                    @change="handleChange">
                            </el-cascader>
                        </div>
                    </template>
                </el-form-item>


                <el-form-item label="门店名称" prop="sname">
                    <el-input v-model="carForm.sname"></el-input>
                </el-form-item>

                <el-form-item label="门店地址" prop="address">
                    <el-input v-model="carForm.address"></el-input>
                </el-form-item>
<!--                <el-form-item label="添加时间" >-->
<!--                    <el-input v-model="carForm.addtime"></el-input>-->
<!--                </el-form-item>-->

                <el-form-item label="是否是常用门店">
                    <el-select v-model="carForm.ishot" placeholder="请选择配置">
                        <el-option
                                v-for="itemes in statusArray"
                                :key="itemes.id"
                                :label="itemes.status"
                                :value="itemes.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>
<script>
    import { regionDataPlus } from 'element-china-area-data'
    export default {
        name: "Allcar",

        data(){
            return {

                //省市县级联
                options: regionDataPlus,

                insertFormRules: {
                    sname: [
                        {required: true, message: '请输入门店名称', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '请输入地址', trigger: 'blur'},
                    ],
                },
                    editDialogVisible: false,
                    //条件搜索对象
                    carFormData: {},
                    //表格数据
                    tableData: [],
                    //每页显示的条数
                    pageSize: 5,
                    //当前的显示的页码
                    currentPage: 1,
                    //总条数
                    total: 0,
                    //添加表:
                    carForm: {},
                    statusArray: [{id: 1, status: "是"}, {id: 2, status: "否"}],

            }
        },
        created() {
            this.selectAllCar();
            this.allselectlist(); //全部下拉框
        },
        methods:{
            //省市县级联
            handleChange(){
                this.carForm.provinceId=this.selectedOptions[0];
                this.carForm.cityId=this.selectedOptions[1];
                this.carForm.areaId=this.selectedOptions[2];

            },

            insert(){
                this.editDialogVisible=true;
                this.carForm={};
                this.selectedOptions=[];
            },
            deleteById(id){
                var that=this;
                this.$http.get(`lease/carShop/deleteCarShop/${id}`).then(function (returnResult) {
                    that.$message.success("删除成功");
                    that.selectAllCar();
                    // location.reload();
                })
                this.currentPage=1;
            },

            //回显
            selectById(row){
                var that=this;
                //console.log("广东我AUS")
                console.log(row);
                that.editDialogVisible=true;

                var idd = row.id;
                this.$http.get(`/lease/carShop/selectCarShopById/${idd}`).then(function (r) {
                    that.selectedOptions=r.data.result.City;
                    that.carForm=r.data.result.tbShop;
                    console.log(r)

                })
                // this.$http.get(`lease/car/selectCarById/${id}`).then(function (returnResult) {
                //     //that.editDialogVisible=true;
                //     // console.log(returnResult);
                //     that.carForm=returnResult.data.result.tbCar;
                //
                // })
            },
            formSubmit(){
                var that=this;
                if(this.carForm.id!=null){
                    this.$http.put("lease/carShop/updateCarShopById",this.carForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.carForm={};
                        that.selectAllCar();
                    })
                }else {
                    this.$http.put("lease/carShop/insertCarShop", this.carForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.selectAllCar();
                    })
                }
            },
            allselectlist(){
                var that=this;
                // this.$http.get("lease/carConfig/selectAllCarConfig").then(function (returnResult) {
                //     that.configArray=returnResult.data.result.AlltbConfigs;
                // })
                // this.$http.get("/lease/carBrand/selectAllCarBrand").then(function (returnResult) {
                //     that.brandsArray=returnResult.data.result.AlltbBrands;
                // })
                // this.$http.get("/lease/carType/selectAllCarType").then(function (returnResult) {
                //     that.typeArray=returnResult.data.result.AlltbTypes;
                // })
                // this.$http.get("/lease/carShop/selectAllCarShop").then(function (returnResult) {
                //     that.shopArray=returnResult.data.result.AlltbShops;
                // })
            },
            chaxun(){
                this.selectAllCar();
            },
            selectAllCar(){
                var that=this;
                this.$http.post(`/lease/carShop/initCarShopTable/${this.currentPage}/${this.pageSize}`,this.carFormData).then(function (returnResult) {
                    console.log(returnResult)
                    that.tableData=returnResult.data.result.records;
                    that.total=returnResult.data.result.total;
                })
            },
            // 改变条数
            handleSizeChange(v){
                this.pageSize=v;
                this.selectAllCar();
            },
            //改变页码
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectAllCar();
            },
            zhuangtaima(row,column){
                var status=row.ishot;
                if(status==1){
                    return '是'
                }
                if(status==2){
                    return '否'
                }
            },
            handleClose()
            {
                this.editDialogVisible=false;
                this.$refs.insertFormRef.resetFields();
            }
        }
    }
</script>

